<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'secure/access_securities.php';
class Control_authors extends Access_securities {
	public function __construct(){
		parent::__construct("control-authors");
		$this -> form_validation -> set_error_delimiters('<p class="help-block danger">', '</p>');
	}
	public function index()
	{
		$data['title']="Authors";
		
		$data['authors']=$this->model_globals->select_status("authors")->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function create_author()
	{
		$data['title']="Authors";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		

		$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[authors.username]');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[authors.email]');
		$this -> form_validation -> set_rules('phone', 'Phone', 'required');
		$this -> form_validation -> set_rules('position', 'Position', 'required');
		
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required');
		
		
		if ($this -> form_validation -> run() === FALSE) {
			$data['modules']=$this->model_globals->select_all("modules")->result();
			$this->load->view(B_MASTER."index",$data);
		} else {

			$data=array(
				'username'=>$this->input->post('username',TRUE),
				'email'=>$this->input->post('email',TRUE),
				'phone'=>$this->input->post('phone',TRUE),
				'position'=>$this->input->post('position',TRUE),
				'address'=>$this->input->post('address',TRUE),
				'password'=>sha1($this->input->post('password',TRUE)),
				'created_date'=>null
			);
			
			$modules=$this->model_globals->select_all("modules");
			$module_access = $this -> input -> post("module_access");
			$module_modify = $this -> input -> post("module_modify");

			// define array
			$access_permission = array();
			$modify_permission = array();
			
			foreach ($module_access as $acc_mod_id) {
				array_push($access_permission, $acc_mod_id);
			}
			foreach ($module_modify as $mod_mod_id) {
				array_push($modify_permission, $mod_mod_id);
			}

			$access=array();
			$modify=array();
			
			foreach($modules->result() as $r_module){
				$module_id=$r_module->module_id;

				if (in_array($module_id, $access_permission)){
				 	array_push($access,"YES"); 
				}else{
				  	array_push($access, "NO");
				 }
				
				if (in_array($module_id, $modify_permission)){
				 	array_push($modify,"YES"); 
				}else{
				  	array_push($modify, "NO");
				 }
			}
			
			// Create author 
			$author_id = $this -> model_globals -> insert_get_last_id("authors",$data);
			
			$i=0;
			foreach($modules->result() as $r_module){
				$module_id=$r_module->module_id;
				$data=array(
					'using_author_id'=>$author_id,
					'using_module_id'=>$module_id,
					'access'=>$access[$i],
					'modify'=>$modify[$i],
					'created_date'=>null
				);
				$this->model_globals->insert("permissions",$data);
				$i++;
			}
			
			$this -> session -> set_flashdata('success', 'New author  was created success !.');
			redirect('control-authors');
			
		}
	}
	public function update_author($author_id=0)
	{
		$data['title']="Authors";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		

		$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
		$this -> form_validation -> set_rules('phone', 'Phone', 'required');
		$this -> form_validation -> set_rules('position', 'Position', 'required');
		
		if ($this -> form_validation -> run() === FALSE) {
			$data['modules']=$this->model_globals->select_all("modules")->result();
			$data['permissions']=$this->model_globals->select_where("permissions",array("using_author_id"=>$author_id));
			$data['author']=$this->model_globals->select_where("authors",array("author_id"=>$author_id))->row();
			$this->load->view(B_MASTER."index",$data);
		} else {

			$data=array(
				'username'=>$this->input->post('username',TRUE),
				'phone'=>$this->input->post('phone',TRUE),
				'position'=>$this->input->post('position',TRUE),
				'address'=>$this->input->post('address',TRUE)
			);
			
			$modules=$this->model_globals->select_all("modules");
			$module_access = $this -> input -> post("module_access");
			$module_modify = $this -> input -> post("module_modify");

			// define array
			$access_permission = array();
			$modify_permission = array();
			
			foreach ($module_access as $acc_mod_id) {
				array_push($access_permission, $acc_mod_id);
			}
			foreach ($module_modify as $mod_mod_id) {
				array_push($modify_permission, $mod_mod_id);
			}

			$access=array();
			$modify=array();
			
			foreach($modules->result() as $r_module){
				$module_id=$r_module->module_id;

				if (in_array($module_id, $access_permission)){
				 	array_push($access,"YES"); 
				}else{
				  	array_push($access, "NO");
				 }
				
				if (in_array($module_id, $modify_permission)){
				 	array_push($modify,"YES"); 
				}else{
				  	array_push($modify, "NO");
				 }
			}
			
			// update author 
			$this->model_globals->update("authors",$data,array("author_id"=>$author_id));
			
			$i=0;
			foreach($modules->result() as $r_module){
				$module_id=$r_module->module_id;
				$data=array(	
					'access'=>$access[$i],
					'modify'=>$modify[$i]
				);
				$this->model_globals->update("permissions",$data,array('using_author_id'=>$author_id,'using_module_id'=>$module_id,));
				$i++;
			}
			
			$this -> session -> set_flashdata('success', 'Author account  was updated success !.');
			redirect('control-authors');
			
		}
	}
	public function delete_author($author_id=0)
	{
		$this->model_globals->delete("authors",array("author_id"=>$author_id));
		$this -> session -> set_flashdata('success', 'Author account  was deleted success !.');
		redirect('control-authors');
	}
	public function disable_author($author_id=0)
	{
		$this->model_globals->update("authors",array("status"=>0),array("author_id"=>$author_id));
		$this -> session -> set_flashdata('success', 'Author account  was disabled success !.');
		redirect('control-authors');
	}
	public function enable_author($author_id=0)
	{
		$this->model_globals->update("authors",array("status"=>1),array("author_id"=>$author_id));
		$this -> session -> set_flashdata('success', 'Author account  was enabled success !.');
		redirect('control-authors');
	}
	public function profile()
	{
		$data['title']="My Profile";
		$data['author']=$this->model_globals->select_where("authors",array("author_id"=>$this->session->userdata("author_id")));
		$this->load->view(B_MASTER."index",$data);
	}
	public function change_password(){
		$data['title']="Change Password";
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required');
		
		if ($this -> form_validation -> run() === FALSE) {
			$this->load->view(B_MASTER."index",$data);
		}else{
			$password=$this->input->post("password",TRUE);
			$this->model_globals->update("authors",array("password"=>sha1($password),array("author_id"=>$this->session->userdata("author_id"))));
			$this -> session -> set_flashdata('success', 'Author password  was changed success !.');
			redirect('control-authors');
		} 
	}
	public function change_picture(){
		$data['title']="Change Picture";
		$this->load->view(B_MASTER."index",$data);
	}
	public function show_author($author_id=0){
		$data['title']="Show Author";
		$data['modules']=$this->model_globals->select_all("modules")->result();
		$data['permissions']=$this->model_globals->select_where("permissions",array("using_author_id"=>$author_id));
		$data['author']=$this->model_globals->select_where("authors",array("author_id"=>$author_id))->row();
		$this->load->view(B_MASTER."index",$data);
	}
	public function view_profile(){
		//$result=$this->model_globals->select_where("authors",array("author_id"=>$this->session->userdata("author_id")));
		//$data=$result->result();
		 $result = mysql_query("SELECT username,email,phone,position,address FROM mw_authors where author_id=".$this->session->userdata("author_id"));          //query
  		$array = mysql_fetch_row($result);    
		echo json_encode($array);
	}
	public function update_profile(){
		$data=array(
			"username"=>$this->input->post("username"),
			"email"=>$this->input->post("email"),
			"phone"=>$this->input->post("phone"),
			"position"=>$this->input->post("position"),
			"address"=>$this->input->post("address"),
		);
		$this->model_globals->update("authors",$data,array("author_id"=>$this->session->userdata("author_id")));
		echo "You have modified success !";
	}
}
